/*
 * @Author: szx
 * @Date: 2021-11-20 18:54:55
 * @LastEditTime: 2021-11-20 19:22:35
 * @Description:
 * @FilePath: \leetcode\500-599\594\594.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
var findLHS = function (nums) {
    nums.sort((a, b) => a - b);
    let ans = 0,
        l = 0;

    for (let i = 0; i < nums.length; i++) {
        while (nums[i] - nums[l] > 1) {
            l++;
        }
        if (nums[i] - nums[l] == 1) {
            ans = Math.max(i - l + 1, ans);
        }
    }
    return ans;
};

var findLHS = function (nums) {
    const cnt = new Map();
    let res = 0;
    for (const num of nums) {
        cnt.set(num, (cnt.get(num) || 0) + 1);
    }
    for (const key of cnt.keys()) {
        if (cnt.has(key + 1)) {
            res = Math.max(res, cnt.get(key) + cnt.get(key + 1));
        }
    }
    return res;
};

console.log(findLHS([-3, -1, -1, -1, -3, -2]));
